import axios from "axios";
import { Message } from "element-ui";
import Cookie from "js-cookie";
const defaultHttp = axios.create({
  baseURL: "http://127.0.0.1:9001/api",
  timeout: 5000,
});

defaultHttp.interceptors.request.use(
  (config) => {
    const token = Cookie.get("token");
    if (token) {
      config.headers.Authorization = token;
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

defaultHttp.interceptors.response.use(
  (response) => {
    if (response.data.code == 1) {
      Message.error(
        response.data.message,
      );
    }
    return response.data;
  },
  (error) => {
    if (error.message.includes("timeout")) {
      Message.error({
        duration: 2,
        message: "接口请求超时,请刷新页面重试!",
      });
    }
    return Promise.reject(error);
  }
);

export default defaultHttp;
